/**
 * Copyright ©2016-2022 tianyu education Corporation, All Rights Reserved
 * http://www.whty.com.cn/
 */
package profile.hujia.redis.common.config;

import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import profile.hujia.redis.dao.model.JedisClusterConfigAttribute;
import profile.hujia.redis.dao.model.JedisCommonConfigAttribute;
import profile.hujia.redis.manager.JedisClusterCache;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;

import java.util.HashSet;
import java.util.Set;

/**
 * class info
 *
 * @author hujia
 * @date 2019-12-24 10:58
 */
@Configuration
public class TestJedisClusterConfig {

    @Bean
    public JedisClusterCache jedisClusterCache(JedisClusterConfigAttribute attribute, JedisCommonConfigAttribute common) {
        JedisClusterCache jedisClusterCache = new JedisClusterCache();
        jedisClusterCache.setJedisClusterCache(jedisCluster(attribute,common));
        return jedisClusterCache;
    }

    @Bean
    public JedisCluster jedisCluster(JedisClusterConfigAttribute attribute, JedisCommonConfigAttribute common){
        Set<HostAndPort> nodes = new HashSet<>();
        for (int i = 0; i < attribute.getIps().size(); ++i) {
            nodes.add(new HostAndPort(attribute.getIps().get(i), attribute.getPorts().get(i)));
        }
        GenericObjectPoolConfig pool = new GenericObjectPoolConfig();
        pool.setMaxIdle(common.getMaxIdle());
        pool.setMinIdle(common.getMinIdle());
        pool.setMaxTotal(common.getMaxTotal());
        pool.setMaxWaitMillis(common.getMaxWaitMillis());
        return new JedisCluster(nodes, common.getTimeout(), pool);
    }
}